package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Permission;
import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IPermissionService;
import cn.wolfcode.wms.service.IRoleService;
import cn.wolfcode.wms.service.ISystemmenuService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private ISystemmenuService systemmenuService;
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){

        PageResult result = roleService.queryForList(qo);


        model.addAttribute("result",result);
        return "role/list";
    }

    @RequestMapping("input")
    public String input(Model model,Long id){
        List<Permission> permissions = permissionService.selectall();
            model.addAttribute("permissions",permissions);
            //菜单
            model.addAttribute("menus",systemmenuService.queryAll());

        if (id != null) {
            Role entity = roleService.get(id);
            model.addAttribute("entity",entity);
        }
        return "role/input";
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Role entity,Long[]permissIds,Long[]menuIds){
        System.out.println(permissIds);
      roleService.saveOrUpdate(entity,permissIds,menuIds);
        return new JSONResult();
    }
    @ResponseBody
    @RequestMapping("delete")
    public Object delete(Long id){

       roleService.delete(id);
        JSONResult json = new JSONResult();
        return json;
    }
}
